let windowWidth = 0;
const chineseNumber = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'];

const formatMonth = date => {
  const month = date.getMonth() + 1;
  const day = date.getDate();
  return month + '月' + day + '日';
}

const formatDate = (date, withoutYear, withWeek) => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  let result = month + '月' + day + '日';
  if (!withoutYear) {
    result = year + '年' + result;
  }
  if (withWeek) {
    let week;
    switch (date.getDay()) {
      case 0: week = '星期天'; break;
      case 1: week = '星期一'; break;
      case 2: week = '星期二'; break;
      case 3: week = '星期三'; break;
      case 4: week = '星期四'; break;
      case 5: week = '星期五'; break;
      case 6: week = '星期六'; break;
      default: week = '无效星期';
    } 
    result += '，' + week
  }
  return result;
}

const formatDateTime = date => {
  const month = date.getMonth() + 1;
  const day = date.getDate();
  let hour = date.getHours().toString();
  let minute = date.getMinutes().toString();
  if (hour.length < 2) hour = '0' + hour;
  if (minute.length < 2) minute = '0' + minute;
  return month + '月' + day + '日 ' + hour + ':' + minute;
}

const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const formatChineseDate = (month, day) => {
  let date = '';
  if (month < 11) {
    date = chineseNumber[month - 1];
  } else if (month == 11) {
    date = '冬';
  } else if (month == 12) {
    date = '腊';
  }
  date += '月';
  if (day <= 10) {
    date = date + '初' + chineseNumber[day - 1];
  } else {
    day = day.toString();
    let dayNumber = parseInt(day[0]);
    let dayNumber2 = parseInt(day[1]);
    if (dayNumber == 1) {
      date += '十';
    } else if (dayNumber == 2) {
      date += dayNumber2 > 0 ? '廿' : '二十';
    } else if (dayNumber == 3) {
      date += dayNumber2 > 0 ? '卅' : '三十';
    } else {
      date += chineseNumber[dayNumber - 1];
    }
    if (dayNumber2 > 0) date += chineseNumber[dayNumber2 - 1];
  }
  return date;
}

const formatChineseCurrency = (money) => {
  let value = String(money);
  let prefix = '';
  if (value.startsWith('-')) {
    prefix = '-';
    value = value.substring(1);
  }
  if (value.length > 2) {
    let index = value.length - 2;
    return prefix + value.substring(0, index) + '.' + value.substring(index);
  } else if (value.length < 2) {
    return prefix + '0.0' + value;
  } else {
    return prefix + '0.' + value;
  }
}

const currentTime = () => {
  return new Date().getTime();
}

const objToMap = (obj, map) => {
  if (map == null) map = new Map(); else map.clear();
  if (obj != null) {
    for (var key in obj) {
      map.set(key, obj[key]);
    }
  }
  return map;
}

const mapToObj = (map, obj) => {
  if (obj == null) obj = {};
  for (let [k, v] of map) {
    obj[k] = v;
  }
  return obj;
}

const mapToList = (map, list) => {
  if (list == null) list = [];
  for (let [k, v] of map) {
    list.push({ key: k, value: v }); 
  }
  return list;
}

const mergeObj = (obj, merge) => {
  if (merge != null) {
    if (obj == null) return obj
    for (var key in merge) {
      if (obj[key] == null) obj[key] = merge[key];
    }
  }
  return obj;
}

const formatColor = (color, defaultColor = '#000000') => {
  const colorType = typeof(color);
  if (colorType != 'number') {
    try {
      if (colorType != 'string') color = String(color);
      if (color.startsWith('#')) color = color.substring(1);
      color = parseInt(color, 16);
    } catch (e) { return defaultColor }
  }
  color = (color + 0x1000000).toString(16);
  if (color.length > 6) { color = '#' + color.substring(color.length - 6) }
  return color;
}

const chineseStringLength = (str) => {  
  if (str == null) return 0;
  var realLength = 0, len = str.length, charCode = -1;
  for (var i = 0; i < len; i++) {
    charCode = str.charCodeAt(i);
    if (charCode >= 0 && charCode <= 128) realLength += 1;
    else realLength += 2;
  }
  return realLength;
}

const chineseStringLengthIndex = (str, length) => {
  if (str == null) return 0;
  var realLength = 0, len = str.length, charCode = -1;
  for (var i = 0; i < len; i++) {
    charCode = str.charCodeAt(i);
    if (charCode >= 0 && charCode <= 128) realLength += 1;
    else realLength += 2;
    if (realLength >= length) return i;
  }
  return len - 1;
}

const computeFontSize = (str, maxFontSize = 120, minFontSize = 30) => {
  let strLength = chineseStringLength(str);
  if (strLength <= 0) strLength = 1;
  const newSize = maxFontSize * 8 / strLength;
  if (newSize > maxFontSize) {
    return maxFontSize;
  } else if (newSize < minFontSize) {
    return minFontSize;
  } else {
    return newSize;
  }
}

const rpx = (param) => {
  if (windowWidth == 0) {
    let res = wx.getSystemInfoSync();
    windowWidth = res.windowWidth;
  }
  return Number((windowWidth / 750 * param).toFixed(2));
}

const groupArray = (data, cols) => {
  const list = [];
  let current = [];

  data.forEach(t => {
    current.push(t);
    if (current.length === cols) {
      list.push(current);
      current = [];
    }
  });

  if (current.length > 0) {
    list.push(current);
  }
  return list;
}

/*
const hashCode = (str: String) => {
  var h = 0;
  var len = str.length;
  var t = 2147483648;
  for (var i = 0; i < len; i++) {
    h = 31 * h + str.charCodeAt(i);
    if (h > 2147483647) h %= t;
  }
  return h;
}*/

module.exports = {
  formatChineseCurrency: formatChineseCurrency,
  formatTime: formatTime,
  formatDate: formatDate,
  formatDateTime: formatDateTime,
  formatChineseDate: formatChineseDate,
  formatColor: formatColor,
  currentTime: currentTime,
  objToMap: objToMap,
  mapToObj: mapToObj,
  mapToList: mapToList,
  mergeObj: mergeObj,
  chineseStringLength: chineseStringLength,
  chineseStringLengthIndex: chineseStringLengthIndex,
  computeFontSize: computeFontSize,
  rpx: rpx,
  groupArray: groupArray
}
